/*
 * Implementation of transmitting messages in CBOR format
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#ifndef CBOR_COMM_H
#define CBOR_COMM_H

#include <stdint.h>
#include <stdbool.h>
#include "qtsm_lib_comm.h"

enum req_type {
    DESCRIBE_PCR = 0,
    EXTEND_PCR = 1,
    LOCK_PCR = 2,
    LOCK_PCRS = 3,
    DESCRIBE_QTSM = 4,
    ATTESTATION = 5,
    ERROR_CODE = 7,
};
typedef enum req_type req_type;

struct req_describe_pcr {
    uint16_t index;
};
typedef struct req_describe_pcr req_describe_pcr;

struct res_describe_pcr {
    bool locked;
    uint32_t data_len;
    uint8_t *data;
};
typedef struct res_describe_pcr res_describe_pcr;

struct req_extend_pcr {
    uint16_t index;
    uint32_t data_len;
    uint8_t *data;
};
typedef struct req_extend_pcr req_extend_pcr;

struct res_extend_pcr {
    uint32_t data_len;
    uint8_t *data;
};
typedef struct res_extend_pcr res_extend_pcr;

struct req_lock_pcr {
    uint16_t index;
};
typedef struct req_lock_pcr req_lock_pcr;

struct res_lock_pcr {
    bool locked;
};
typedef struct res_lock_pcr res_lock_pcr;

struct req_lock_pcrs {
    uint32_t count;
    uint16_t *range;
};
typedef struct req_lock_pcrs req_lock_pcrs;

struct res_lock_pcrs {
    bool locked;
};
typedef struct res_lock_pcrs res_lock_pcrs;

struct req_attestation {
    uint32_t user_data_len;
    uint8_t *user_data;
    uint32_t nonce_len;
    uint8_t *nonce;
    uint32_t pubkey_len;
    uint8_t *pubkey;
};
typedef struct req_attestation req_attestation;

struct res_attestation {
    uint32_t data_len;
    uint8_t *data;
};
typedef struct res_attestation res_attestation;

struct qtsm_message_req {
    req_type type;
    union {
        struct req_describe_pcr describe_pcr;
        struct req_extend_pcr extend_pcr;
        struct req_lock_pcr lock_pcr;
        struct req_lock_pcrs lock_pcrs;
        struct req_attestation attestation;
    } qtsm_message_req_u;
};
typedef struct qtsm_message_req qtsm_message_req;

struct qtsm_message_res {
    req_type type;
    union {
        struct res_describe_pcr describe_pcr;
        struct res_extend_pcr extend_pcr;
        struct res_lock_pcr lock_pcr;
        struct res_lock_pcrs lock_pcrs;
        struct describe_qtsm describe_qtsm;
        struct res_attestation attestation;
        enum error_code error_num;
    } qtsm_message_res_u;
};
typedef struct qtsm_message_res qtsm_message_res;

#endif
